﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HF.BaseWebCtrlLib;
using HF.Business.Common;
using HF.Framework.Web;
using HF.Business.Finance;
using HF.WorkFlow.Const;
using HF.Framework.DataClientDBAgent;


public partial class UserControls_Finance_Evection : BaseUserControl
{
    DataTable EvectionDetTable;
    string SnEvectionDet = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load();
        SnEvectionDet = WorkflowInsId;//
        if (CtrlState == WorkFlowConst.STATE_VIEW)//控制编辑区是否显示
        {
            
            Panel2.Visible = false;
        }
        else
        {
           
            Panel2.Visible = true;
        }
        if (!IsPostBack)
        {
            initData();

        }
        EvectionDetTable = (DataTable)Session[SnEvectionDet];

    }
    private void initData()
    {
        DataTable EvectionTable = Evection.Getfin_EvectionTable(WorkflowInsId);
        if (EvectionTable != null && EvectionTable.Rows.Count > 0)//检查是否有数据
        {
            Evection sr = new Evection();
            sr.Getfin_EvectionInfo(WorkflowInsId);
            tbxBillCode.Text = sr.BillCode;
            YYSubmitDate.Text = sr.SubmitDate;
            tbxSubmitUser.Text = sr.SubmitUser;
            tbxDept.Text = sr.SubmitDept;
            tbxReason.Text = sr.Reason;
            tbxTotal.Text = sr.total.ToString();
            tbxCntotal.Text = sr.cntotal;
            tbxFactprice.Text = sr.factprice.ToString();
            tbxLendprice.Text = sr.lendprice.ToString();
            tbxRemark.Text = sr.Remark;
            EvectionDetTable = EvectionDet.Getfin_EvectionDetTable(WorkflowInsId);
        }
        else
        {
            string billType = "FCCB";
            tbxBillCode.Text = billType + BusinessComm.GetBillCode(billType);
            YYSubmitDate.Text = DateTime.Now.ToShortDateString();
            tbxSubmitUser.Text = this.UserName;
            tbxDept.Text = this.ArchCaption;
            ClientDBAgent agent = new ClientDBAgent();
            EvectionDetTable = agent.GetBlankTable("fin_EvectionDet");
        }
        GridView1.DataSource = EvectionDetTable;
        GridView1.DataBind();
        Session.Add(SnEvectionDet, EvectionDetTable);
    }
    private void DataBindSource()
    {
        GridView1.DataSource = EvectionDetTable;
        GridView1.DataBind();
        Session.Add(SnEvectionDet, EvectionDetTable);

    }
    private void  sumTotal()
    {
        double total = 0.00;
        double vehicelcost = 0.00;//车票费
        double livecost = 0.00;//住宿费
        double citycost = 0.00;//市内交通费
        double evectioncost = 0.00;//补贴
        double others=0.00;//其他费
        double lendprice = 0.00;//借款

        foreach (DataRow dr in EvectionDetTable.Rows)
        {
            vehicelcost = 0.00;//车票费
            livecost = 0.00;//住宿费
            citycost = 0.00;//市内交通费
            evectioncost = 0.00;//补贴
            others=0.00;//其他费
            lendprice = 0.00;//借款
            if (dr["vehicelcost"].ToString().Length != 0)
                vehicelcost = Convert.ToDouble(dr["vehicelcost"]);
            if (dr["citycost"].ToString().Length != 0)
                citycost = Convert.ToDouble(dr["citycost"]);
            if (dr["liveprice"].ToString().Length != 0 && dr["livedays"].ToString().Length != 0)
            {
                livecost = Convert.ToDouble(dr["livedays"]) * Convert.ToDouble(dr["liveprice"]);
            }
            if (dr["evectiondays"].ToString().Length != 0 && dr["allowance"].ToString().Length != 0)
            {
                evectioncost = Convert.ToDouble(dr["evectiondays"]) * Convert.ToDouble(dr["allowance"]);
            } 
             if (dr["others"].ToString().Length != 0)
                others = Convert.ToDouble(dr["others"]);

            total = total + vehicelcost + citycost + livecost + evectioncost + others;
        }
        tbxTotal.Text = total.ToString();
        if (tbxLendprice.Text.Length != 0)
            lendprice = Convert.ToDouble(tbxLendprice.Text);
        tbxFactprice.Text = Convert.ToString(Math.Round(total - lendprice, 2));

    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            
            if (tbxStartaddress.Text.Length == 0)
            {
                WorkDialog.ShowMessage(this.Page, "请输入起点。");
                return;
            }
            if (tbxEndaddress.Text.Length == 0)
            {
                WorkDialog.ShowMessage(this.Page, "请输入终点。");
                return;
            }
            
            DataRow dr = EvectionDetTable.NewRow();
            dr["WorkflowId"] = WorkflowId;
            dr["WorkflowInsId"] = WorkflowInsId;
            dr["WorktaskId"] = WorktaskId;
            dr["WorktaskInsId"] = WorktaskInsId;
            dr["BillCode"] = tbxBillCode.Text;
            dr["startaddress"] =tbxStartaddress.Text;
            dr["endaddress"] = tbxEndaddress.Text;
            dr["vehicle"] = drpVehicle.SelectedValue;
            dr["vehicelcost"] = tbxVehicelcost.Text;
            dr["citycost"] = tbxCitycost.Text ;
            dr["livedays"] = tbxLivedays.Text;
            dr["liveprice"] = tbxLiveprice.Text;
            dr["evectiondays"] = tbxEvectiondays.Text;
            dr["allowance"] = tbxAllowance.Text;
            dr["others"] = tbxOthers.Text;
            EvectionDetTable.Rows.Add(dr);
            DataBindSource();
            sumTotal();
        }
        catch (Exception ex)
        {
            WorkDialog.ShowMessage(this.Page, "增加失败:" + ex.Message.ToString());
        }
    }
    protected void btnModify_Click(object sender, EventArgs e)
    {
        try
        {
            
            if (tbxStartaddress.Text.Length == 0)
            {
                WorkDialog.ShowMessage(this.Page, "请输入起点。");
                return;
            }
            if (tbxEndaddress.Text.Length == 0)
            {
                WorkDialog.ShowMessage(this.Page, "请输入终点。");
                return;
            }
            
            DataRow dr = EvectionDetTable.Rows[GridView1.SelectedIndex];

            dr["WorkflowId"] = WorkflowId;
            dr["WorkflowInsId"] = WorkflowInsId;
            dr["WorktaskId"] = WorktaskId;
            dr["WorktaskInsId"] = WorktaskInsId;
            dr["BillCode"] = tbxBillCode.Text;
            dr["startaddress"] = tbxStartaddress.Text;
            dr["endaddress"] = tbxEndaddress.Text;
            dr["vehicle"] = drpVehicle.SelectedValue;
            dr["vehicelcost"] = tbxVehicelcost.Text;
            dr["citycost"] = tbxCitycost.Text;
            dr["livedays"] = tbxLivedays.Text;
            dr["liveprice"] = tbxLiveprice.Text;
            dr["evectiondays"] = tbxEvectiondays.Text;
            dr["allowance"] = tbxAllowance.Text;
            dr["others"] = tbxOthers.Text;

      
            DataBindSource();
            sumTotal();
        }
        catch (Exception ex)
        {
            WorkDialog.ShowMessage(this.Page, "增加失败:" + ex.Message.ToString());
        }
    }
    private void saveEvectionDet()
    {
        EvectionDet.Delete(WorkflowInsId);//首先删除已有的数据
        foreach (DataRow dr in EvectionDetTable.Rows)
        {
            EvectionDet srd = new EvectionDet();
            srd.fedId = Guid.NewGuid().ToString();
            srd.WorkflowId = dr["WorkflowId"].ToString();
            srd.WorkflowInsId = dr["WorkflowInsId"].ToString();
            srd.WorktaskId = dr["WorktaskId"].ToString();
            srd.WorktaskInsId = dr["WorktaskInsId"].ToString();
            srd.BillCode = dr["BillCode"].ToString();
            srd.startaddress = dr["startaddress"].ToString();
            srd.endaddress = dr["endaddress"].ToString();
            srd.vehicle = dr["vehicle"].ToString();
            srd.vehicelcost = Convert.ToDouble(dr["vehicelcost"].ToString());
            srd.citycost = Convert.ToDouble(dr["citycost"]);
            srd.livedays = Convert.ToDouble(dr["livedays"]);
            srd.liveprice = Convert.ToDouble(dr["liveprice"]);
            srd.evectiondays = Convert.ToDouble(dr["evectiondays"]);
            srd.allowance = Convert.ToDouble(dr["allowance"]);
            srd.others = Convert.ToDouble(dr["others"]);
            srd.Insert();
        }
    }
    public override void SaveUserControl(bool IsDraft)
    {
        base.SaveUserControl(IsDraft);
        Evection sr = new Evection();
        sr.WorkflowId = WorkflowId;
        sr.WorkflowInsId = WorkflowInsId;
        sr.WorktaskId = WorktaskId;
        sr.WorktaskInsId = WorktaskInsId;
        sr.BillCode = tbxBillCode.Text;
        sr.SubmitDate = YYSubmitDate.Text;
        sr.SubmitUser = tbxSubmitUser.Text;
        sr.SubmitDept = tbxDept.Text;
        sr.total = Convert.ToDouble(tbxTotal.Text);
        if (tbxLendprice.Text.Length != 0)
            sr.lendprice = Convert.ToDouble(tbxLendprice.Text);
        if (tbxFactprice.Text.Length != 0)
        sr.factprice = Convert.ToDouble(tbxFactprice.Text);
        sr.Reason = tbxReason.Text;
        sr.Remark = tbxRemark.Text;

        DataTable edt= Evection.Getfin_EvectionTable(WorkflowInsId);
        if (edt!=null&&edt.Rows.Count>0)//检查是否有数据。
            sr.Update();
        else
            sr.Insert();
        saveEvectionDet();

    }
    protected void tbxDuty_TextChanged(object sender, EventArgs e)
    {

    }
    protected void TextBox8_TextChanged(object sender, EventArgs e)
    {

    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        if (CtrlState == WorkFlowConst.STATE_VIEW) return;
        EvectionDetTable.Rows.Remove(EvectionDetTable.Rows[e.RowIndex]);
       
        DataBindSource();
        sumTotal();
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow gvr = GridView1.SelectedRow;
        tbxStartaddress.Text = gvr.Cells[1].Text;
        tbxEndaddress.Text = gvr.Cells[2].Text;
        drpVehicle.Text = gvr.Cells[3].Text;
        tbxVehicelcost.Text = gvr.Cells[4].Text;
        tbxCitycost.Text = gvr.Cells[5].Text;
        tbxLivedays.Text= gvr.Cells[6].Text;
        tbxLiveprice.Text = gvr.Cells[7].Text;
        tbxEvectiondays.Text = gvr.Cells[8].Text;
        tbxAllowance.Text = gvr.Cells[9].Text;
        tbxOthers.Text = gvr.Cells[10].Text;
    }
}
